package cn.zjutleo.village_server.service;

import cn.zjutleo.village_server.entity.EchartsData.OrderDataOption;
import cn.zjutleo.village_server.mapper.EchartsDataMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author: 张坤
 * @DateTime: 2021/9/9 15:53
 * @Description: 该类用于 查询数据表
 */
@Service
public class EchartsDataService {

    @Resource
    EchartsDataMapper echartsDataMapper;

    /**
     * @Author: 张坤
     * @Description: 查询派单和报修的数据对比,数据举例如下
     * source: [
     *             ['product', '2015', '2016'],//这个不是数据,是数据的属性
     *             ['Matcha Latte', 43.3, 85.8],
     *             ['Milk Tea', 83.1, 73.4],
     *             ['Cheese Cocoa', 86.4, 65.2],
     *             ['Walnut Brownie', 72.4, 53.9]
     *           ]
     */
    public List<String[]> selectOrderData(String addressName){
        List<String[]> result=new ArrayList<>();
        //存入数据属性
        String[] attribute=new String[]{"type","用户报修","派单"};
        result.add(attribute);
        List<OrderDataOption> RepairApplyData= echartsDataMapper.selectRepairApplyData(addressName);
        List<OrderDataOption> DispatchRepairyData= echartsDataMapper.selectDispatchRepairyData(addressName);
        // 算法开销有点大,不过数据库查询到的数据量应该不是很多,这样应该问题不大
        int noDataNumber=0;// 这个是在报修中查到的但是在派单中没查到的数据数量,主要用于减少算法时间开销
        for(int i=0;i<RepairApplyData.size();i++){
            String[] newData=new String[3];
            newData[0]=RepairApplyData.get(i).getTypeName();
            newData[1]= String.valueOf(RepairApplyData.get(i).getCount());
            boolean flag=false;
            for (int j=i-noDataNumber;j<DispatchRepairyData.size();j++){
                if(RepairApplyData.get(i).getTypeName().equals(DispatchRepairyData.get(j).getTypeName())){
                    newData[2]= String.valueOf(DispatchRepairyData.get(j).getCount());
                    flag=true;
                    break;
                }
            }
            if(!flag){
                noDataNumber++;
                newData[2]="0";
            }
            result.add(newData);
        }
        return result;
    }
}
